
	var id = GetQueryString("id");

	//菜单树
	var menu_ztree;
	var menu_setting = {
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: -1
			},
			key: {
				url:"nourl"
			}
		},
		check:{
			enable:true,
			nocheckInherit:true
		}
	};
	
	//部门结构树
	/*var dept_ztree;
	var dept_setting = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "id",
	            pIdKey: "parentId",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    }
	};*/

	//数据树
	var data_ztree;
	var data_setting = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "id",
	            pIdKey: "parentId",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    },
	    check:{
	        enable:true,
	        nocheckInherit:true,
	        chkboxType:{ "Y" : "", "N" : "" }
	    }
	};
	
	var vm = new Vue({
    	  el: '#app',
    	  data: {
    		  role:{
    				
    			}
    	  },
    	  methods: {
    		  getRole: function(roleId){
    	            $.get(baseURL + "sys/role/info/"+roleId, function(r){
    	            	vm.role = r.role;
    	                
    	                //勾选角色所拥有的菜单
    	    			var menuIds = vm.role.menuIdList;
    	    			for(var i=0; i<menuIds.length; i++) {
    	    				var node = menu_ztree.getNodeByParam("id", menuIds[i]);
    	    				menu_ztree.checkNode(node, true, false);
    	    			}

    	                //勾选角色所拥有的部门数据权限
    	                /*var deptIds = vm.role.deptIdList;
    	                for(var i=0; i<deptIds.length; i++) {
    	                    var node = data_ztree.getNodeByParam("id", deptIds[i]);
    	                    data_ztree.checkNode(node, true, false);
    	                }*/

    	                //vm.getDept();
    	    		});
    			},
    			saveOrUpdate: function() {
    				//获取选择的菜单
    				var nodes = menu_ztree.getCheckedNodes(true);
    				var menuIdList = new Array();
    				for(var i=0; i<nodes.length; i++) {
    					menuIdList.push(nodes[i].id);
    				}
    				vm.role.menuIdList = menuIdList;

    	            //获取选择的数据
    	            //var nodes = data_ztree.getCheckedNodes(true);
    	            /*var deptIdList = new Array();
    	            for(var i=0; i<nodes.length; i++) {
    	                deptIdList.push(nodes[i].id);
    	            }
    	            vm.role.deptIdList = deptIdList;*/
    				
    				var url = vm.role.id == null ? "sys/role/save" : "sys/role/update";
    				$.loadAjax({
    					type: "POST",
    				    url: baseURL + url,
    	                contentType: "application/json",
    				    data: JSON.stringify(vm.role),
    				    success: function(r){
    				    	if(r.code === 0){
    							alert('提交成功', function(index){
    								backToTable();
    				    			layer.close(index);
    							});
    						}else{
    							alert(r.msg);
    						}
    					}
    				});
    			},
    			getMenuTree: function(roleId) {
    				//加载菜单树
    				$.get(baseURL + "sys/menu/list", function(r){
    					menu_ztree = $.fn.zTree.init($("#menuTree"), menu_setting, r);
    					//展开所有节点
    					menu_ztree.expandAll(true);
    					
    					if(roleId != null){
    						vm.getRole(roleId);
    					}
    				});
    		    }/*,
    	        getDataTree: function(roleId) {
    	            //加载菜单树
    	            $.get(baseURL + "sys/dept/list", function(r){
    	                data_ztree = $.fn.zTree.init($("#dataTree"), data_setting, r);
    	                //展开所有节点
    	                data_ztree.expandAll(true);
    	            });
    	        },
    	        getDept: function(){
    	            //加载部门树
    	            $.get(baseURL + "sys/dept/list", function(r){
    	                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting, r);
    	                var node = dept_ztree.getNodeByParam("id", vm.role.deptId);
    	                if(node != null){
    	                    dept_ztree.selectNode(node);

    	                    vm.role.deptName = node.name;
    	                }
    	            })
    	        },
    	        deptTree: function(){
    	            layer.open({
    	                type: 1,
    	                offset: '50px',
    	                title: "选择部门",
    	                area: ['300px', '400px'],
    	                shade: 0,
    	                shadeClose: false,
    	                content: jQuery("#deptLayer"),
    	                btn: ['确定', '取消'],
    	                btn1: function (index) {
    	                    var node = dept_ztree.getSelectedNodes();
    	                    //选择上级部门
    	                    vm.role.deptId = node[0].id;
    	                    vm.role.deptName = node[0].name;

    	                    layer.close(index);
    	                }
    	            });
    	        }*/
    	  },
    	  mounted:function(){
              	
    		//this.getDept();
    		//this.getDataTree();
		  	if (id != null && id.length > 0) {
		  		// id 存在则为编辑
		  		this.getMenuTree(id);
		  	} else {
		  		// 添加
		  		//this.role = {deptName:null, deptId:null};
		  		this.getMenuTree(null);
		  	}
		  	
    	  }
    });
	
	//表单验证
	$(function() {

        $("#editForm").validate({
            rules: {
            	roleName: "required"
                
            },
            messages: {
            	roleName: "请输入角色名"
            },
            submitHandler : function(form) {  //验证通过后的执行方法
                //当前的form通过ajax方式提交
                vm.saveOrUpdate();
            },
            /* 重写错误显示消息方法,以tips方式弹出错误消息 */   
            showErrors: function(errorMap, errorList) {   
            	var msg = "";   
                $.each( errorList, function(i,v){   
                  msg = (v.message+"\r\n");   
                  if(msg!="") layer.tips(msg, "#"+v.element.id, { tips:2, tipsMore: true });   
                });   
            },   
            /* 提示失去焦点时不验证 */    
            onfocusout: false 
        });
    });
        
        